package com.kailu.hotel.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kailu.hotel.model.CustomerModel;
import com.kailu.hotel.model.ForRoomUsingModel;
import com.kailu.hotel.model.HistoryModel;
import com.kailu.hotel.model.RoomModel;
import com.kailu.hotel.model.RoomUsingModel;
import com.kailu.hotel.model.ServiceUsingModel;
import com.kailu.hotel.service.CustomerManager;
import com.kailu.hotel.service.HistoryManager;
import com.kailu.hotel.service.RoomManager;
import com.kailu.hotel.service.RoomUsingManger;
import com.kailu.hotel.service.ServiceManager;
import com.kailu.hotel.service.ServiceUsingManger;

/**
 * @author viet.ch
 * @generated Apr 30, 2014
 * @from VN
 */

@Controller
public class RoomUsingController {
	
	@Autowired
	RoomManager roomManager;
	
	@Autowired
	RoomUsingManger ruManager;
	
	@Autowired
	CustomerManager customerManager;
	
	@Autowired
	ServiceManager serviceManager;
	
	@Autowired
	ServiceUsingManger suManager;
	
	@Autowired
	HistoryManager historyManager;
	
	ForRoomUsingModel fru = new ForRoomUsingModel();

	@RequestMapping("/roomusing")
	public String getRoomUsing(ModelMap map){
		map.addAttribute("roomList",roomManager.getAllRoom());
		map.addAttribute("ruList",ruManager.getAllRoomUsing());
		return "roomusing";
	}
	
	@RequestMapping(value = "/roomusing/{room_id}", method = RequestMethod.GET)
    public String getRoomUsingDetail(@PathVariable("room_id") int room_id,ModelMap map)
    {
		int isUsed = 0;
		if (ruManager.getRoomUsing(room_id) != null) {
			fru.setRoomUsingModel(ruManager.getRoomUsing(room_id));
			fru.setCustomerModel(customerManager.getCustomer(ruManager.getRoomUsing(room_id).getCustomer_id())
					);
			isUsed = 1;
		} else {
			RoomUsingModel rum = new RoomUsingModel();
			rum.setRoom_id(room_id);
			fru.setRoomUsingModel(rum);
			fru.setCustomerModel(new CustomerModel());
		}
		
		if (suManager.getServiceUsing(room_id) != null){
//			map.addAttribute("serviceusinglist",suManager.getServiceUsing(room_id));
			
			System.out.println("!null");
			
			map.addAttribute("serviceusinglist",suManager.getServiceUsing(room_id));
		} else {
			//map.addAttribute("serviceusinglist",new ServiceUsingModel());
			
			System.out.println("null");
		}
		
		map.addAttribute("serviceList",serviceManager.getAllService());
		map.addAttribute("allmodel", fru);
		map.addAttribute("id",room_id);
		map.addAttribute("isUsed",isUsed);
        return "roomusing_detail";
    }
	
	@RequestMapping(value = "/roomusing/{room_id}/fail", method = RequestMethod.GET)
    public String getRoomUsingDetail_fail(@PathVariable("room_id") int room_id,ModelMap map)
    {
		int isUsed = 0;
		if (ruManager.getRoomUsing(room_id) != null) {
			fru.setRoomUsingModel(ruManager.getRoomUsing(room_id));
			fru.setCustomerModel(customerManager.getCustomer(ruManager.getRoomUsing(room_id).getCustomer_id())
					);
			isUsed = 1;
		} else {
			RoomUsingModel rum = new RoomUsingModel();
			rum.setRoom_id(room_id);
			fru.setRoomUsingModel(rum);
			fru.setCustomerModel(new CustomerModel());
		}
		
		if (suManager.getServiceUsing(room_id) != null){
//			map.addAttribute("serviceusinglist",suManager.getServiceUsing(room_id));
			
			System.out.println("!null");
			
			map.addAttribute("serviceusinglist",suManager.getServiceUsing(room_id));
		} else {
			//map.addAttribute("serviceusinglist",new ServiceUsingModel());
			
			System.out.println("null");
		}
		
		map.addAttribute("serviceList",serviceManager.getAllService());
		map.addAttribute("allmodel", fru);
		map.addAttribute("id",room_id);
		map.addAttribute("isUsed",isUsed);
		map.addAttribute("errorMessage","Không đủ số lượng");
        return "roomusing_detail";
    }
	
	@RequestMapping(value = "/roomusing/detail", method = RequestMethod.POST)
	public String addOrUpdate(@ModelAttribute("allmodel") ForRoomUsingModel model,BindingResult result,ModelMap map)
	{
		ForRoomUsingModel forRU = new ForRoomUsingModel();
		String url = "roomusing_detail";
		model.getCustomerModel().setCustomer_id(model.getRoomUsingModel().getCustomer_id());
		
		//if cus_id null,
		if (customerManager.getCustomer(model.getRoomUsingModel().getCustomer_id()) == null 
				&& model.getCustomerModel().getCustomer_name().isEmpty()
				&& model.getCustomerModel().getCustomer_address().isEmpty()) {
			forRU.setCustomerModel(new CustomerModel());
			forRU.setRoomUsingModel(model.getRoomUsingModel());
		}
		
		if (customerManager.getCustomer(model.getRoomUsingModel().getCustomer_id()) != null 
				&& model.getCustomerModel().getCustomer_name().isEmpty()
				&& model.getCustomerModel().getCustomer_address().isEmpty()) {
			forRU.setCustomerModel(customerManager.getCustomer(model.getRoomUsingModel().getCustomer_id()));
			forRU.setRoomUsingModel(model.getRoomUsingModel());
		}
		
		//ok
		if ( !model.getCustomerModel().getCustomer_name().isEmpty()
				&& !model.getCustomerModel().getCustomer_address().isEmpty()) {
			customerManager.updateCustomer(model.getCustomerModel());
			if (!model.getRoomUsingModel().getIn_date().isEmpty()) {
				forRU.setCustomerModel(customerManager.getCustomer(model.getCustomerModel().getCustomer_id()));
				ruManager.addRoomUsing(model.getRoomUsingModel());
				url="redirect:/roomusing/"+model.getRoomUsingModel().getRoom_id();
			} else {
				forRU.setRoomUsingModel(model.getRoomUsingModel());
			}
		}
		map.addAttribute("allmodel",forRU);
		map.addAttribute("serviceusinglist",suManager.getServiceUsing(model.getRoomUsingModel().getRoom_id()));
		return url;
	}
	
	@RequestMapping(value = "/roomusing/pay/{room_id}", method = RequestMethod.GET)
    public String getPayment(@PathVariable("room_id") int room_id,ModelMap map) throws ParseException{
		
		RoomUsingModel rum = ruManager.getRoomUsing(room_id);
		RoomModel room = roomManager.getRoom(room_id);

		//cal room money
		String indate = rum.getIn_date();
		int room_price = room.getRoom_price();
		
		Date date = new Date();

		String format = "EEE MMM dd kk:mm:ss z yyyy";
		String indateformat = "yyyy-MM-dd HH:mm:ss";

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		SimpleDateFormat sdf2 = new SimpleDateFormat(indateformat);

		Date dateObj1 = (Date) sdf2.parse(indate);
		Date dateObj2 = (Date) sdf.parse(date.toString());

		long diff = dateObj2.getTime() - dateObj1.getTime();
//		double diffInHours = diff / ((double) 1000 * 60 * 60);

		int DD = (int) (diff / (24 * 60 * 60 * 1000));
		long HH = (diff / (60 * 60 * 1000) % 24);
		
		//cal time by day
		int day_money = DD*room_price;
		
		//cal hours
		int hour_money =0;
		
		if (HH >=2) {
			hour_money = (int) ((HH - 1)*(room_price*0.1) + room_price*0.4);
		} else {
			hour_money = (int) (room_price*0.4);
		}
		
		if (hour_money > room_price) {
			hour_money= room_price;
		}
		
		int room_money = day_money+hour_money;
		
		System.out.println("Gio vao ori: " + indate);
		System.out.println("Gio vao : " + dateObj1);
		System.out.println("Gio ra : " + dateObj2);
		
		System.out.println("So Ngay"+DD);
		System.out.println("So gio" + HH);
		System.out.println("Gia tien"+room_price);
		System.out.println("Tien phong"+room_money);
		//end calc room money
		
		//start cal service money
		int su_money = 0;
		for (ServiceUsingModel serviceList : suManager.getServiceUsing(room_id)) {
			su_money = su_money + serviceList.getNumber() * serviceManager.getServiceById(serviceList.getService_id()).getService_price();
		}
		
		System.out.println("Tien dich vu: " + su_money);
		
		HistoryModel history = new HistoryModel();
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String out_date = df.format(date);
		
		//update model 
		history.setRoom_id(room_id);
		history.setCustomer_id(ruManager.getRoomUsing(room_id).getCustomer_id());
		history.setIn_date(indate);
		history.setOut_date(out_date);
		history.setNumber(ruManager.getRoomUsing(room_id).getNumber());
		history.setRoom_money(room_money);
		history.setService_money(su_money);
		history.setTotal_money(su_money + room_money);
		
		map.addAttribute("customer",customerManager.getCustomer(ruManager.getRoomUsing(room_id).getCustomer_id()));
		map.addAttribute("history",history);
		map.addAttribute("serviceUsingList",suManager.getServiceUsing(room_id));
		map.addAttribute("room",room);
		map.addAttribute("day",DD);
		map.addAttribute("hour",HH);
		map.addAttribute("serviceList",serviceManager.getAllService());
		
		//clear data
		historyManager.addHistory(history);
		for (ServiceUsingModel	model : suManager.getServiceUsing(room_id)) {
			suManager.deleteServiceUsing(model.getService_using_id());
		}
		ruManager.deleteRoomUsing(room_id);
		
		return "bill";
	}
	
	@RequestMapping("/roomusing/bill")
	public String fillReport(ModelMap map){
		return "bill";
	}
}
